package com.heyu.cgsd.ui.activity;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heyu.cgsd.R;
import com.heyu.cgsd.base.BaseActivity;
import com.heyu.cgsd.ui.adapter.BaseRVAdapter;
import com.heyu.cgsd.ui.dialog.CustomDialog;
import com.heyu.cgsd.util.SpeedConvertUtils;
import com.heyu.cgsd.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 速度转换
 */
public class SpeedActivity extends BaseActivity {
    private TextView tv_title;
    private ImageView iv_return;
    private LinearLayout ll_sp_1, ll_sp_2, ll_sp_3;
    private TextView tv_sp_1, tv_sp_2, tv_sp_3;
    private int sp_pos_1, sp_pos_2, sp_pos_3;
    private List<Object[]> objectList;

    private TextView num_1, num_2, num_3, unit_1, unit_2, unit_3;

    private TextView kb_num_0, kb_num_1, kb_num_2, kb_num_3, kb_num_4, kb_num_5, kb_num_6, kb_num_7, kb_num_8, kb_num_9, kb_num_dot, kb_num_ac;
    private LinearLayout kb_num_del;

    @org.jetbrains.annotations.Nullable
    @Override
    protected Integer getContentView() {
        setTitleBackground(BG_BLACK);
        return R.layout.activity_speed;
    }

    @Override
    public void initView() {
        tv_title = findViewById(R.id.tv_title);
        iv_return = findViewById(R.id.img_back);

        ll_sp_1 = findViewById(R.id.ll_sp_1);
        ll_sp_2 = findViewById(R.id.ll_sp_2);
        ll_sp_3 = findViewById(R.id.ll_sp_3);

        tv_sp_1 = findViewById(R.id.tv_sp_1);
        tv_sp_2 = findViewById(R.id.tv_sp_2);
        tv_sp_3 = findViewById(R.id.tv_sp_3);

        num_1 = findViewById(R.id.num_1);
        num_2 = findViewById(R.id.num_2);
        num_3 = findViewById(R.id.num_3);

        unit_1 = findViewById(R.id.unit_1);
        unit_2 = findViewById(R.id.unit_2);
        unit_3 = findViewById(R.id.unit_3);

        kb_num_0 = findViewById(R.id.kb_num_0);
        kb_num_1 = findViewById(R.id.kb_num_1);
        kb_num_2 = findViewById(R.id.kb_num_2);
        kb_num_3 = findViewById(R.id.kb_num_3);
        kb_num_4 = findViewById(R.id.kb_num_4);
        kb_num_5 = findViewById(R.id.kb_num_5);
        kb_num_6 = findViewById(R.id.kb_num_6);
        kb_num_7 = findViewById(R.id.kb_num_7);
        kb_num_8 = findViewById(R.id.kb_num_8);
        kb_num_9 = findViewById(R.id.kb_num_9);
        kb_num_dot = findViewById(R.id.kb_num_dot);
        kb_num_del = findViewById(R.id.kb_num_del);
        kb_num_ac = findViewById(R.id.kb_num_ac);

    }

    public void initData() {
        tv_title.setText("速度转换");

        String[] titles = new String[]{"米/秒", "光速", "马赫", "千米/小时", "千米/秒", "海里/小时", "英里/小时", "英寸/秒", "英尺/秒"};
        String[] units = new String[]{"m/s", "c", "Ma", "km/h", "km/s", "kn", "mph", "ips", "fps"};

        int len = units.length;
        objectList = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            Object[] o = {titles[i], units[i]};
            objectList.add(o);
        }

        sp_pos_1 = 0;
        sp_pos_2 = 1;
        sp_pos_3 = 2;

        tv_sp_1.setText((String) (objectList.get(0))[0]);
        tv_sp_2.setText((String) (objectList.get(1))[0]);
        tv_sp_3.setText((String) (objectList.get(2))[0]);

        unit_1.setText(units[0]);
        unit_2.setText(units[1]);
        unit_3.setText(units[2]);

        startConvert();
        initListener();
    }

    protected void initListener() {
        iv_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ll_sp_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog(1);
            }
        });

        ll_sp_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog(2);
            }
        });

        ll_sp_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog(3);
            }
        });

        setKeyBoardListener();

        num_1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                startConvert();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void setKeyBoardListener() {
        kb_num_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('0');
            }
        });

        kb_num_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('1');
            }
        });

        kb_num_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('2');
            }
        });

        kb_num_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('3');
            }
        });

        kb_num_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('4');
            }
        });

        kb_num_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('5');
            }
        });

        kb_num_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('6');
            }
        });

        kb_num_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('7');
            }
        });

        kb_num_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('8');
            }
        });

        kb_num_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('9');
            }
        });

        kb_num_dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                formatInput('.');
            }
        });

        kb_num_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = StringUtils.replaceSeparate(num_1.getText().toString());
                if (!str.equals("0")) {
                    str = str.substring(0, str.length() - 1);
                    if (str.equals("")) {
                        str = "0";
                    }
                }
                formatNum(str);
            }
        });

        kb_num_ac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num_1.setText("0");
                num_2.setText("0");
                num_3.setText("0");
            }
        });
    }


    private void formatInput(char input) {
        String str = StringUtils.replaceSeparate(num_1.getText().toString());

        if (input >= '0' && input <= '9') {
            if ((str.contains(".") && str.length() < 20) || (!str.contains(".") && str.length() < 15)) {
                if (str.equals("0")) {
                    str = String.valueOf(input);
                } else {
                    str += String.valueOf(input);
                }
                formatNum(str);
            }
        } else if (input == '.') {
            if (!str.contains(".") && str.length() < 15) {
                str += ".";
                formatNum(str);
            }
        }
    }


    private void formatNum(String str) {
        num_1.setText(StringUtils.formatInteger(str));
    }

    private void startConvert() {
        String str = StringUtils.replaceSeparate(num_1.getText().toString());
        double result2 = SpeedConvertUtils.convert(Double.valueOf(str), sp_pos_1, sp_pos_2);
        double result3 = SpeedConvertUtils.convert(Double.valueOf(str), sp_pos_1, sp_pos_3);

        num_2.setText(StringUtils.formatToSeparate3(String.valueOf(result2)));
        num_3.setText(StringUtils.formatToSeparate3(String.valueOf(result3)));
    }

    private void showCustomDialog(final int type) {
        View view = getLayoutInflater().inflate(R.layout.dialog_spinner, null);
        final CustomDialog customDialog = new CustomDialog(this, 0, 0, view, R.style.DialogTheme);
        TextView tv_dialog_title = view.findViewById(R.id.tv_dialog_title);
        RecyclerView rv_list = view.findViewById(R.id.rv_list);
        TextView cancel = view.findViewById(R.id.tv_dialog_cancel);

        tv_dialog_title.setText("选择单位");

        BaseRVAdapter rvAdapter = new BaseRVAdapter<Object[]>(objectList) {

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_spinner;
            }

            @Override
            public void convert(VH holder, Object[] data, int position) {
                holder.setText(R.id.item_spinner_name, (String) data[0]);
                holder.setText(R.id.item_spinner_unit, (String) data[1]);
            }
        };

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SpeedActivity.this);

        rv_list.setLayoutManager(linearLayoutManager);
        rv_list.setAdapter(rvAdapter);

        if (rv_list.getItemDecorationCount() <= 0) {
            rv_list.addItemDecoration(new DividerItemDecoration(SpeedActivity.this, DividerItemDecoration.VERTICAL));
        }

        rvAdapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (type == 1) {
                    sp_pos_1 = position;
                    tv_sp_1.setText((String) (objectList.get(position))[0]);
                    unit_1.setText((String) (objectList.get(position))[1]);
                } else if (type == 2) {
                    sp_pos_2 = position;
                    tv_sp_2.setText((String) (objectList.get(position))[0]);
                    unit_2.setText((String) (objectList.get(position))[1]);
                } else if (type == 3) {
                    sp_pos_3 = position;
                    tv_sp_3.setText((String) (objectList.get(position))[0]);
                    unit_3.setText((String) (objectList.get(position))[1]);
                }
                startConvert();
                customDialog.dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDialog.dismiss();
            }
        });

        customDialog.show();
        customDialog.setCanceledOnTouchOutside(true);
//        customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {
//
//            }
//        });
    }
}
